package com.servingcloud.serverb.modules.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.servingcloud.serverb.common.model.RestPage;
import com.servingcloud.serverb.modules.dao.CompanyMapper;
import com.servingcloud.serverb.modules.entity.Company;
import com.servingcloud.serverb.modules.model.CompanyMODIFY;
import com.servingcloud.serverb.modules.model.CompanyQUERY;
import com.servingcloud.serverb.modules.model.CompanySAVE;
import com.servingcloud.serverb.modules.service.CompanyService;
import com.servingcloud.serverb.utils.BeanCopierWrapper;
import com.servingcloud.serverb.utils.IdGenerator;

@Service
public class CompanyServiceImpl implements CompanyService{

    @Autowired
    CompanyMapper companyMapper;
    @Autowired
    IdGenerator idGenerator;//生成id
    public RestPage<Company> findCompanyPage(CompanyQUERY param){
        PageHelper.startPage(param.getPageNo(), param.getPageSize());
        Company obj = new Company();
		BeanCopierWrapper.copy(param, obj);
        List<Company> userList = companyMapper.select(obj);
        PageInfo<Company> pageInfo = new PageInfo<>(userList);
        RestPage<Company> page = new RestPage<Company>();
        page.setTotal(pageInfo.getTotal());
        page.add(pageInfo.getList());
        return page;
    }
	@Override
	public Long saveCompany(CompanySAVE param) {
		Company obj = new Company();
		BeanCopierWrapper.copy(param, obj);
		obj.setCompanyId(idGenerator.nextId());
		companyMapper.insert(obj);
		return obj.getCompanyId();
	}
	@Override
	public void modifyCompany(CompanyMODIFY param) {
		companyMapper.update(param);
	}
	@Override
	public void deleteCompany(CompanyMODIFY param) {
		Company obj = new Company();
		BeanCopierWrapper.copy(param, obj);
		companyMapper.delete(obj);
	}
}